<template>
	<om-page :title="pageTitle">
		<view class="" v-if="!lookFile">
			<view v-if="!getImgUrl" class="page__bd">
				<view v-if="showFileList">
					<uni-list v-for="(item, index) in showFileList" :key="index">
						<uni-list-item 
							:showArrow="false" :showHover="false">
							<view class="flex justify-center align-center">
								<view class="tui-col-9">{{item.fileName}}</view>
								<view class="tui-left tui-col-3">
									<button size="mini" class="om-bg-lanS" @click="itemtap(item)">查看</button>
								</view>
							</view>
						</uni-list-item>
					</uni-list>
				</view>
				<view v-else>
					<uni-list v-for="(item, index) in datas" :key="index">
						<uni-list-item 
							:showArrow="false" :showHover="false">
							<view class="flex justify-center align-center">
								<view class="tui-col-9">{{item.fileName}}</view>
								<view class="tui-left tui-col-3">
									<button size="mini" class="om-bg-lanS" @click="itemtap(item)">查看</button>
								</view>
							</view>
						</uni-list-item>
					</uni-list>
				</view>
			 </view>
			 <om-sub v-else title="图片预览">
				 <view class="flex justify-center align-center">
					<image :src="getImgUrl" mode="aspectFit" @click="imageClick"></image>
					<!-- <ot-cropper :src="cropperUrl" :showCropper="false" @cancel="cancel"></ot-cropper> -->
				 </view>
			 </om-sub>
		</view>
		 <view v-else="lookFile" class="page__bd">
		 	<uni-list v-for="(item, index) in datas" :key="index">
		 		<uni-list-item 
		 			:showArrow="false" :showHover="false">
		 			<view class="flex justify-center align-center">
		 				<view class="tui-col-9">{{item.uploadName}}</view>
		 				<view class="tui-left tui-col-3">
		 					<button size="mini" class="om-bg-lanS" @click="openFileUrl(item)">查看</button>
		 				</view>
		 			</view>
		 		</uni-list-item>
		 	</uni-list>
		  </view>
	</om-page>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl:"",
				fileUrl:'uploadFiles/getFile.do',
				mainId:'',
				subId:'',
				busCode:"",//业务职能编码
				fileId:'',
				simpleClazzName:'',
				fieldId:'',
				dataUrl:"uploadFiles/findFileList",
				code:"",//表单编码
				datas:[],
				pageTitle:"",
				cropperUrl:"",
				lookFile:"",
				fileUplaodKey:'',
				showFileList:''
			}
		},
		computed:{
			getImgUrl(){
				if(this.imgUrl){
					return this.imgUrl
				}
				if(this.busCode && this.fileId && this.simpleClazzName && this.fileUplaodKey){
					return this.om.getUrl(this.fileUrl) + "?busCode="+this.busCode + "&fileId="+this.fileId+"&simpleClazzName="+this.simpleClazzName+'&uploadFileKey='+this.fileUplaodKey
				}
				return ""
			}
		},
		methods: {
			openFileUrl(item){
				let url = this.om.getUrl(item.fileUrl)
				let obj = {
					attr:{
						pdfFileUrl:url
					}
				}
				if(item.uploadName.endsWith("pdf")){
					uni.navigateTo({
						url: "/pages/public/pdf-preview/pdf-preview?filePath="+item.fileUrl
					})
				}else{
					this.openImage(url)
				}
			},
			itemtap: function(item) {
				let url = this.om.getUrl(this.fileUrl) + "?busCode="+this.busCode+"&fileId="+item.id+"&simpleClazzName="+this.simpleClazzName+'&uploadFileKey='+this.fileUplaodKey
			  	var obj = {
					attr:{
						busCode:this.busCode,
						fileId:item.id,
						simpleClazzName:this.simpleClazzName
					},
					fileUplaodKey:this.fileUplaodKey
				}
				item.fileName.endsWith("pdf")?this.om.openPdf(obj):(this.openImage(url))
			},
			openImage(fileUrl){
				uni.setStorage({
					key:"image-cache",
					data:fileUrl
				})
				uni.navigateTo({
					url:'../img-view/img-view'
				})
			},
			imageClick(){
				uni.previewImage({
					urls:[this.getImgUrl],
					current:0
				})
			},
			cancel() {
				this.cropperUrl="";
			}
		},
		onLoad(options) {
			this.pageTitle = options.title||"文件列表"
			uni.setNavigationBarTitle({
			  title: this.pageTitle
			})
			this.showFileList = uni.getStorageSync('showFileList')
			if(this.showFileList){
				uni.removeStorageSync('showFileList')
			}
			if(options.fileUrl){
				this.imgUrl = options.fileUrl
			}else if(options.lookFile){
				this.lookFile = options.lookFile
				uni.getStorage({
					key:"fileListCache",
					success:(res)=>{
						if(res.data){
							this.datas = res.data
						}
						uni.removeStorage({
							key:'fileListCache'
						})
					}
				})
			}else{
				if(options.busCode){
					this.busCode = options.busCode
				}
				if(options.fileId){
					this.fileId = options.fileId
				}
				if(options.simpleClazzName){
					this.simpleClazzName = options.simpleClazzName
				}
				if(options.fieldId){
					this.fieldId = options.fieldId
				}
				if(options.fileUplaodKey){
					this.fileUplaodKey = options.fileUplaodKey
				}
				if(!this.fileId){
					if(options.mainId){
						this.mainId = options.mainId
					}
					if(options.subId){
						this.subId = options.subId
					}
					let _that = this
					let param = {
						busCode:this.busCode,
						mainId:this.mainId,
						subId:this.subId,
						simpleClazzName:this.simpleClazzName,
						uploadFileKey:this.fileUplaodKey
					}
					this.om.request(this.dataUrl,param,"post").then(res=>{
						//if(res instanceof Array)
						if(Object.prototype.toString.call(res)==='[object Array]')
							_that.datas = res
						//else if(res.value instanceof Array){
						else if(Object.prototype.toString.call(res.value)==='[object Array]'){
							_that.datas = res.value
						}
					})
				}
			}
			
		}
	}
</script>

<style>

</style>
